var mongoose = require('mongoose');
var config = require('../config/config.js');
// console.log(config.mongooseConfig);
// mongoose.connect(config.mongooseConfig);

/**
 * [connect description]  Connect to mongodb
 * @return {[type]} [description]
 */
var connect = function() {
	var options = {
		server: {
			socketOptions: {
				keepAlive: 1 // 允许保持连接数
			}
		}
	};
	mongoose.connect(config.mongooseConfig, options);
};

// start connect...
connect();

// Error handler
mongoose.connection.on('error', function(err) {
	console.error('mongodb exception：');
	console.error(err);
});

// Reconnect when closed
var count = 0;
mongoose.connection.on('disconnected', function() {
	console.log('count=' + count);
	count++;
	if(count < 5)
		connect();
});

exports.mongoose = mongoose;